package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        //将前端没有传过来的省名称,城市名称,区域名称

        Long roomInfoId = roomSubmitVo.getId();
        if (ObjectUtils.isNotEmpty(roomInfoId)) {
            // 需要将配套的房间内容删除,然后再更新新内容 通过roomInfoId进行全部删除
            deleteRoomInfoDetailById(roomInfoId);
        }
        // 接下来进行更新或者插入操作
        super.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();
        // 1接下来将新的配套信息进行插入操作
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                BeanUtils.copyProperties(graphVo, graphInfo);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        // 2接下来进行属性信息列表的插入操作
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValues = new ArrayList<>();
            attrValueIds.forEach(attrValueId -> {
                RoomAttrValue build = RoomAttrValue.builder().roomId(roomId).attrValueId(attrValueId).build();
                roomAttrValues.add(build);
            });
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        // 3接下来进行配套信息的插入操作
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilities = new ArrayList<>();
            facilityInfoIds.forEach(facilityInfoId -> {
                RoomFacility build = RoomFacility.builder().roomId(roomId).facilityId(facilityInfoId).build();
                roomFacilities.add(build);
            });
            roomFacilityService.saveBatch(roomFacilities);
        }

        //4接下来进行标签信息批量插入操作
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> roomLabels = new ArrayList<>();
            labelInfoIds.forEach(labelInfoId -> {
                RoomLabel build = RoomLabel.builder().roomId(roomId).labelId(labelInfoId).build();
                roomLabels.add(build);
            });
            roomLabelService.saveBatch(roomLabels);
        }
        // 5接下来进行支付方式列表的插入操作
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            paymentTypeIds.forEach(paymentTypeId -> {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(build);
            });
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        // 6接下来进行可选租期列表的插入操作
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            leaseTermIds.forEach(leaseTermId -> {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(build);
            });
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public void removeRoomInfoDetailById(Long id) {
        //删除前需要判读是否有租约,有租约不能删除
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, id);
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ROOM_HAS_USER);
        }
        // 删除房间信息后,要同步删除房间的配套关联信息
        super.removeById(id);
        // 删除相应的配套信息
        deleteRoomInfoDetailById(id);
    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        Long apartmentId = roomInfo.getApartmentId();
        //查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, apartmentId));
        roomDetailVo.setApartmentInfo(apartmentInfo);
        // 查询图片列表信息
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphQueryWrapper);
        if (!CollectionUtils.isEmpty(graphInfoList)) {
            List<GraphVo> graphVoList = new ArrayList<>();
            graphInfoList.forEach(graphInfo -> {
                GraphVo graphVo = new GraphVo();
                BeanUtils.copyProperties(graphInfo, graphVo);
                graphVoList.add(graphVo);
            });
            roomDetailVo.setGraphVoList(graphVoList);
        }
        //查询属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        // 查询配套信息列表
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectFacilityInfoListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        //查询标签信息列表
        List<LabelInfo> labelInfos = labelInfoMapper.selectListByRoomInfoIdAndItemType(id);
        roomDetailVo.setLabelInfoList(labelInfos);
        //查询支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomInfoId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        // 查询可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomInfoId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }


    private void deleteRoomInfoDetailById(Long id) {
        // 1 删除房间对应的图片信息列表
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(queryWrapper);
        // 2 删除属性信息关联表
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);
        // 3 删除房间配套信息列表
        LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityQueryWrapper);
        // 4 删除标签信息列表
        LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelQueryWrapper);
        // 5 删除支付方式列表
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
        // 6 删除可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
    }
}




